package org.zxl.springbootdemo.poi.controller;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.zxl.springbootdemo.poi.word.ExportWord2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class HelloController {

    @PostMapping("/upload") // 等价于 @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String uplaod( @RequestParam("file") MultipartFile file, Model m) {
        try {
            ExportWord2 ew = new ExportWord2();
            InputStream[] pictureFiles = new InputStream[]{file.getInputStream()};
            XWPFDocument document = ew.createXWPFDocument("经济内循环", pictureFiles);

            String realFileName="example";
            ServletRequestAttributes servletRequestAttributes =
                    (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
            HttpServletResponse response = servletRequestAttributes.getResponse();
            response.setContentType("application/msword;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;fileName=" + realFileName + ".docx");
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");

//            File excelFile = new File(realFileName);
//            res.setCharacterEncoding("UTF-8");
//            res.setHeader("content-type", "application/octet-stream;charset=UTF-8");
//            res.setContentType("application/octet-stream;charset=UTF-8");
//            //加上设置大小下载下来的.xlsx文件打开时才不会报“Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃”
//            res.addHeader("Content-Length", String.valueOf(excelFile.length()));
//            try {
//                res.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(realFileName.trim(), "UTF-8"));
//            } catch (UnsupportedEncodingException e1) {
//                e1.printStackTrace();
//            }
            OutputStream os  = response.getOutputStream();
            document.write(os);
            os.flush();
            os.close();
            System.out.println("导出完成");

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } finally {
        }
        System.out.println("文档生成成功");

        return null;
    }

    @RequestMapping("/hello")
    public String hellojsp(){
        System.out.println("111111111");
        return "index";
    }

    @GetMapping("/add")
    public String add(){
        return "add";
    }

    @GetMapping("/rest")
    @ResponseBody
    public String rest(){
        return "i am rest service;";
    }
}
